/**
 * 
 */
package com.yst.metho.web.application.vacationday;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import com.yst.metho.web.application.interfaces.IListingViewModel;
import com.yst.metho.web.application.utils.AbstractSortableTableModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.metho.web.application.vacationday.actions.CreateVacation;
import com.yst.metho.web.application.vacationday.actions.EditVacationDayAction;

/**
 * @author perso
 * 
 */
public class VacationListModel extends AbstractSortableTableModel implements IListingViewModel
{
	private String[] mColumnsName =
	{ "Jour", "Mois", "Annee" };

	private Collection mVacationList;

	public VacationListModel() throws NamingException, FinderException, CreateException
	{
		super();
		refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#getToolbarAction()
	 */
	public List getToolbarAction()
	{
		ArrayList actions = new ArrayList();
		actions.add(new CreateVacation(this));
		actions.add(new EditVacationDayAction(this));
		return actions;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.interfaces.IListingViewModel#refresh()
	 */
	public void refresh()
	{
		try
		{
			mVacationList = Locator.getVacationView().findVacationDays();
			fireTableDataChanged();
		}
		catch (NamingException e)
		{
			throw new RuntimeException(e);
		}
		catch (FinderException e)
		{
			throw new RuntimeException(e);
		}
		catch (CreateException e)
		{
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.AbstractTableModel#getColumnName(int)
	 */
	public String getColumnName(int arg0)
	{
		return mColumnsName[arg0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getColumnCount()
	 */
	public int getColumnCount()
	{
		return mColumnsName.length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getRowCount()
	 */
	public int getRowCount()
	{
		int retour = 0;
		if (null != mVacationList)
		{
			retour = mVacationList.size();
		}
		return retour;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.table.TableModel#getValueAt(int, int)
	 */
	public Object getValueAt(int column, int row)
	{
		Object retour = null;
		Iterator it = mVacationList.iterator();
		int i = -1;
		do
		{
			retour = it.next();
			i++;
		}
		while (i != row);
		return retour;
	}
}
